package com.qidian.md.service.impl;

import com.qidian.md.mapper.ItemTypeMapper;
import com.qidian.md.service.ItemTypeService;
import com.qidian.mes.pojo.md.ItemType;
import com.qidian.mes.util.pojo.MESResult;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 18林梦雅
 * @Company 起点编程
 * @Time 2023/10/27 14:36
 * @Description
 */
@Service
@Transactional
public class ItemTypeServiceImpl implements ItemTypeService{
    @Autowired
    ItemTypeMapper itemTypeMapper;


    @Override
    public MESResult selectAll() {
        List<ItemType> itemTypes = itemTypeMapper.selectAllByParentId(0L);
        for(ItemType itemType : itemTypes){
            List<ItemType> children = selectChildren(itemType.getItemTypeId());// //遍历查看有没有 孩子
            itemType.setChildren(children);
        }
        return MESResult.ok(itemTypes);
    }
    public List<ItemType> selectChildren(Long parentTypeId){
        List<ItemType> childrens = itemTypeMapper.selectAllByParentId(parentTypeId);//通过父节点查询 孩子
        for(ItemType  children : childrens ){//遍历
            List<ItemType> childs = selectChildren(children.getItemTypeId());//遍历查看有没有 孩子
            children.setChildren(childs);
        }
        return  childrens;
    }

    @Override
    public MESResult queryList(ItemType itemType) {

        return MESResult.ok( itemTypeMapper.queryList(itemType));

    }

    @Override
    public MESResult deleteById(Long itemTypeId) {
        return MESResult.ok(itemTypeMapper.deleteById(itemTypeId));
    }



    /**
     * 先根据Id查询内容,返回给前端的编辑页面
     * @param itemTypeId
     * @return
     */
    @Override
    public MESResult queryById(Long itemTypeId) {
        ItemType itemType = null;
        if ((itemType = itemTypeMapper.selectById(itemTypeId)) != null){
            return MESResult.ok(itemType);
        }
        return MESResult.empty();
    }
    /**
     * 根据物料产品分类id-->物料产品分类修改编辑
     * 直接更新数据,不用判断物料产品分类名是否重复,编码是不一样的
     * @param itemType
     * @return
     */
    @Override
    public MESResult update(ItemType itemType) {

        return MESResult.updateIsOk(itemTypeMapper.update(itemType));

    }

    /**
     * 添加物料产品分类,先判断物料产品分类名是否重复,再添加
     * @param itemType
     * @return
     */
    @Override
    public MESResult addItemType(ItemType itemType) {
//
        return MESResult.updateIsOk(itemTypeMapper.addItemType(itemType));
    }

    @Override
    public MESResult selectByname(ItemType itemType) {
        return MESResult.ok(itemTypeMapper.selectByItemTypeName(itemType.getItemTypeName()));
    }


}
